package models

import "gorm.io/gorm"

type Video struct {
	gorm.Model
	Title  string `gorm:"type:varchar(30);index"json:"title"`
	Author string `gorm:"type:varchar(10);index"json:"author"`
	Zan    int    `json:"zan"`
	Cate   string `json:"cate"`
	Img    string `gorm:"type:varchar(255)" json:"img"`
}

// 视频添加

func VideoCreate(video *Video) error {
	err = DB.Create(&video).Error
	if err != nil {
		return err
	} else {
		return nil
	}
}

// 视频分页

func VideoPage(page, pageSize int, title string) ([]Video, int64, error) {
	var videos []Video
	var total int64
	query := DB.Model(Video{})
	if title != "" {
		query = query.Where("title like ?", "%"+title+"%")
	}
	if err = query.Count(&total).Error; err != nil {
		return nil, 0, err
	}
	if err = query.Order("zan desc").Offset((page - 1) * pageSize).Limit(pageSize).Find(&videos).Error; err != nil {
		return nil, 0, err
	}
	return videos, total, nil
}

// 视频详情
